<?php
class CommonController extends Controller
{
	
	public function actionProxy(){
		if(!isset($_GET['url'])){
			echo "{errorCode:1,message:'error：no url'}";
			die();
		}else{
			$cookie = "";
			foreach ($_COOKIE as $name => $value) {
				$cookie = $cookie."" . $name . "=" . $value . "; ";
			}
			$output = Mod::app()->curl->setOption(CURLOPT_COOKIE, $cookie)->get($url,array());
			$cgi_url = $_GET['url'];
			$output = Mod::app()->curl->post($cgi_url,$_POST);
			echo $output;
			die();
		}
	}
	public function actionPicture(){
		$attach = CUploadedFile::getInstanceByName('file');  
	    $retValue = "";  
	    if($attach->size > 3*1024*1024){  
	        $retValue = "提示：文件大小不能超过3M";  
	    }else{
	    	$file_name = md5(file_get_contents($attach->tempName));
	    	$image = Yii::app()->image->load($attach->tempName);
		    //var_dump($image->width);
		    //var_dump($image->height);
			$image->save(Yii::app()->basePath . '/../upload/'. $file_name .".jpg");

			$this->responseJSON(0,array(
				"filename"=>"". $file_name .".jpg"
				));
	    }  
	    $this->responseJSON(500,$retValue);

		/*
	    $image = Yii::app()->image->load('images/test.jpg');
		$image->resize(400, 100)->rotate(-45)->quality(75)->sharpen(20);
		$image->save(); // or $image->save('images/small.jpg');

		$uploadFile = CUploadedFile::getInstance($model, 'pic_address');
		if($uploadFile !== null){
		$uploadFileName = mktime() . '.' . $uploadFile->getExtensionName();
		$model->pic_address = $uploadFileName;
		}
		if($model->save()){
		$uploadFile->saveAs(Yii::app()->basePath . '/../../upload/'.$uploadFileName);
		
			$this->redirect(array('view','id'=>$model->id));
		}
		*/
	}
	public function actionUpload(){
		echo $_FILES["file"]["size"] ;
		if ((($_FILES["file"]["type"] == "image/gif")
		|| ($_FILES["file"]["type"] == "image/jpeg")
		|| ($_FILES["file"]["type"] == "image/pjpeg"))
		&& ($_FILES["file"]["size"] < 200000))
		{
			if ($_FILES["file"]["error"] > 0)
			{
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
			}
			else
			{
				echo "Upload: " . $_FILES["file"]["name"] . "<br />";
				echo "Type: " . $_FILES["file"]["type"] . "<br />";
				echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
				echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
				echo Yii::app()->basePath . '/../assets/upload/';
			
				move_uploaded_file($_FILES["file"]["tmp_name"],
				Yii::app()->basePath . '/../assets/upload/'. time());
				echo "Stored in: " . "upload/" . time();
			}
		}
		else
		{
		echo "Invalid file";
		}
		/*
		$uploadFile = CUploadedFile::getInstance($model, 'pic_address');
		if($uploadFile !== null){
		$uploadFileName = mktime() . '.' . $uploadFile->getExtensionName();
		$model->pic_address = $uploadFileName;
		}
		if($model->save()){
		$uploadFile->saveAs(Yii::app()->basePath . '/../../upload/'.$uploadFileName);
		
			$this->redirect(array('view','id'=>$model->id));
		}
		*/
	}
}